package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.BabyHeight;
import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.BabyHeightRepository;
import cn.b.training.baby_education_training.service.BabyHeightService;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.UserInfoService;
import cn.b.training.baby_education_training.service.dto.BabyHeightDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 09:57
 **/
@RestController
@RequestMapping("/height")
public class BabyHeightController {
    private final UserInfoService userInfoService;
    private final BabyHeightService babyHeightService;
    private final BabyHeightRepository babyHeightRepository;
    private final BabyInfoService babyInfoService;

    public BabyHeightController(UserInfoService userInfoService, BabyHeightService babyHeightService, BabyHeightRepository babyHeightRepository, BabyInfoService babyInfoService) {
        this.userInfoService = userInfoService;
        this.babyHeightService = babyHeightService;
        this.babyHeightRepository = babyHeightRepository;
        this.babyInfoService = babyInfoService;
    }

    @PostMapping("/insert/height")
    public ResponseEntity insertHeight(@RequestParam Long babyId,
                                     @RequestParam String recordTime,
                                     @RequestParam float height){
        try{
            Authentication user= SecurityContextHolder.getContext().getAuthentication();
            User currentUser=(User)user.getPrincipal();
            UserInfo userInfo=this.userInfoService.getUser(currentUser.getUsername());
            if(userInfo!=null){
                BabyHeight babyHeight = this.babyHeightService.insertHeight(recordTime,height);
                BabyInfo babyInfo = babyInfoService.getBabyById(babyId);
                if (babyInfo!=null){
                    babyHeight.setBabyId(babyId);
                    babyHeightRepository.save(babyHeight);
                }
                //babyInfo = babyInfoRepository.save(babyInfo);
                return ResponseEntity.ok(babyHeight);
            }else{
                JSONObject error = new JSONObject();
                error.put("error","登录身份不合法");
                return ResponseEntity.status(400).body(error);
            }
        }catch(Exception e){
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @PutMapping("/update/height")
    public ResponseEntity updateHeight(@Valid BabyHeightDto heightDto){
        try {
            boolean b = babyHeightService.updateHeightInfo(heightDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","修改成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","修改失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping("/delete/height")
    public ResponseEntity deleteHeight(@RequestParam Long id){
        try {
            boolean b = babyHeightService.deleteHeight(id);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/get/id")
    public ResponseEntity getHeight(@RequestParam Long id){
        try {
            return ResponseEntity.ok(babyHeightService.getHeightById(id));
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/get/babyId")
    public ResponseEntity getBabyHeight(@RequestParam Long babyId){
        try {
            return ResponseEntity.ok(babyHeightService.getHeightByBabyId(babyId));
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/all")
    public ResponseEntity getHeights(@RequestParam(required = false, defaultValue = "0") Integer page,
                                      @RequestParam(required = false, defaultValue = "10") Integer size) {
        Page<BabyHeight> result = this.babyHeightService.getAllHeights(page, size);
        return ResponseEntity.ok(result);
    }
}
